package com.javaweb.admin.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.javaweb.admin.entity.*;
import com.javaweb.admin.mapper.FormtableMain12974Mapper;
import com.javaweb.admin.mapper.KdlQffAttachMapper;
import com.javaweb.admin.mapper.KdlQffFeedbackMapper;
import com.javaweb.admin.mapper.ScheduledMapper;
import com.javaweb.admin.query.QffFeedbackQuery;
import com.javaweb.admin.service.AbstractJob;
import com.javaweb.admin.service.IFormtableMain12974Service;
import com.javaweb.admin.service.INcTaskLogService;
import com.javaweb.common.annotation.Log;
import com.javaweb.common.config.CommonConfig;
import com.javaweb.common.config.DataSourceType;
import com.javaweb.common.config.DynamicDataSource;
import com.javaweb.common.enums.LogType;
import com.javaweb.common.enums.OperType;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.SFTPUtil;
import com.javaweb.system.service.IDictDataService;
import com.seegw.shaphar.IKDLService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Component
public class DownloadQffAttachJob extends AbstractJob {
    private static final Logger log = LoggerFactory.getLogger(DownloadQffAttachJob.class);
    @Autowired
    ScheduledMapper scheduledMapper;


    @Autowired
    IFormtableMain12974Service formtableMain12974Service;


    @Autowired
    INcTaskLogService iNcTaskLogService;

    @Autowired
    SFTPUtil ftpUtil;

    @Autowired
    private IKDLService kdlService;





    @Override
    public String getName() {
        QueryWrapper<Scheduled> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("classpath",this.getClass().getName());
        Scheduled entity = scheduledMapper.selectOne(queryWrapper);
        return entity.getName();
    }

    @Override
    @Log(title = "下载异常反馈附件",isSaveRequestData = false,operType = OperType.MANAGE,logType = LogType.UPDATE)
    public void execute() {

        //同步异常反馈
        QffFeedbackQuery query = new QffFeedbackQuery();
        query.setPrincipalid(CommonConfig.kdlCompanyId);
        query.setStarttime(kdlService.getQffMaxSyncTime());
        query.setEndtime(DateUtils.getTime());
        kdlService.syncQffFeedback(query);

        List<KdlQffAttach> attachList = kdlService.getQffAttachList();
        NcTaskLog taskLog = new NcTaskLog();
        taskLog.setTaskType("下载异常反馈附件");
        taskLog.setBillCount(attachList.size());
        taskLog.setStartTime(DateUtils.now());
        String billno = "";

        log.info(String.format("开始下载异常反馈附件,获取到%d个待下载附件....", attachList.size()));
        boolean downSuccess = false;
        ftpUtil.connect("sftp");
        for (KdlQffAttach attach : attachList) {
            billno += attach.getAttachfile() + ",";
            downSuccess = ftpUtil.downloadFile(CommonConfig.kdlSftpQFFPath, attach.getAttachfile(), CommonConfig.qffAttachFilePath);
            // 在给OA提供文件目录再下载一份
            downSuccess = ftpUtil.downloadFile(CommonConfig.kdlSftpQFFPath, attach.getAttachfile(), CommonConfig.oaqffAttachFilePath + attach.getQffno() + File.separator);
            if (downSuccess) {
                kdlService.updateDownloadStatus(attach.getId(), "已下载");
                ftpUtil.deleteFile(CommonConfig.kdlSftpQFFPath, attach.getAttachfile());
            }
        }
        ftpUtil.disconnect();
        taskLog.setBillNo(billno);
        taskLog.setEndTime(DateUtils.now());
        iNcTaskLogService.save(taskLog);
        // 更新数据状态oa ftriggerflag ,标记oa可读取状态,和
        formtableMain12974Service.updateFtriggerflag();
        //挑出oa中已归档的数据
        formtableMain12974Service.sendOaToKdl();
    }
}
